
// 初始化导航栏
function initNavigation() {
    $('#linkHome').data("src", "./page/home.html");
    $('#linkProductManager').data("src", "./page/product.html");
    $('#linkUserManager').data("src", "./page/user.html");
    $('#linkCategoryManager').data("src", "./page/category.html");
    $('#linkCartManager').data("src", "./page/cart.html");
    const navItem = $(".nav-link");
    $(".nav-link").click(function () {
        navItem.removeClass('active');
        $(this).addClass('active');
        $('#customIframe').attr('src', $(this).data("src"));
    });
}

// 上传图片
function uploadProductImg(e) {
    var formDate = new FormData();
    formDate.append("file", e.target.files[0]);
    $.ajax({
        url: 'http://localhost:8080/common/upload',
        type: 'post',
        cache: false,
        contentType: false,
        processData: false,
        data: formDate,
        success: function (res) {
            $('#productImgLabel').children().remove();
            const img = '<img id="product_img" alt="图片" class="border border-dark" src="' + res.data + '" style="width: 103px;height: 103px;margin-top: -10px">';
            $('#productImgLabel').append(img);
        }
    })
}

// 上传banner图片
function uploadBannerImg(e) {
    var formDate = new FormData();
    formDate.append("file", e.target.files[0]);
    $.ajax({
        url: 'http://localhost:8080/common/upload',
        type: 'post',
        cache: false,
        contentType: false,
        processData: false,
        data: formDate,
        success: function (res) {
            $('#bannerImgLabel').children().remove();
            const img = '<img id="img" alt="图片" class="border border-dark" src="' + res.data + '" style="width: 103px;height: 103px;margin-top: -10px">';
            $('#bannerImgLabel').append(img);
        }
    })
}

function loadPagingLabel(e){
    var pageLink = e;
    var pages = pageLink.data('page').pages;
    var pageNum = pageLink.data('page').pageNum;
    pageLink.children().remove();
    pageLink.append('<li class="page-item" value="-1"><a class="page-link" aria-label="Previous"><span aria-hidden="true">&laquo;</span></a></li>')
    if (pages <= 6) {
        for (let i = 1; i <= pages; i++) {
            pageLink.append('<li id="page' + i + '" class="page-item" value="' + i + '" ><a class="page-link">' + i + '</a></li>');
        }
    } else if (pages - pageNum >= 3 && pageNum >= 4) {
        pageLink.append('<li id="page' + 1 + '" class="page-item" value="' + 1 + '"><a class="page-link">' + 1 + '</a></li>');
        pageLink.append('<li class="page-item" ><a class="page-link">...</a></li>');
        for (let i = pageNum - 1; i <= pageNum + 1; i++) {
            pageLink.append('<li id="page' + i + '" class="page-item" value="' + i + '" ><a class="page-link">' + i + '</a></li>');
        }
        pageLink.append('<li class="page-item" ><a class="page-link">...</a></li>');
        for (let i = pages - 1; i <= pages; i++) {
            pageLink.append('<li id="page' + i + '" class="page-item" value="' + i + '"><a class="page-link">' + i + '</a></li>');
        }
    } else {
        for (let i = 1; i <= 4; i++) {
            pageLink.append('<li id="page' + i + '" class="page-item" value="' + i + '"><a class="page-link">' + i + '</a></li>');
        }
        pageLink.append('<li class="page-item" ><a class="page-link">...</a></li>');
        for (let i = pages - 3; i <= pages; i++) {
            pageLink.append('<li id="page' + i + '" class="page-item" value="' + i + '" ><a class="page-link">' + i + '</a></li>');
        }
    }
    pageLink.append('<li class="page-item" value="0" ><a class="page-link" aria-label="Next"><span aria-hidden="true">&raquo;</span></a></li>')
}

function setPagingLabel(e) {
    var pageNum = e.data('page').pageNum;
    $('.page-item').each(function (){
        $(this).removeClass('active');
    })
    var getTarget = '#page' + pageNum;
    $(getTarget).addClass('active');
}


// 处理分页的页数
function pageItem(e) {
    var targetPage = $(e).val();
    var pageLink = $('#pageLink');
    var getPageNum = pageLink.data("page").pageNum;
    var maxPage = pageLink.data("page").pages;
    if (targetPage > 0) {
        if (getPageNum == targetPage) {
            return;
        }
        show(targetPage);
        return;
    }
    if (targetPage == 0) {
        targetPage = 1;
    }
    var finalPage = targetPage + getPageNum;
    if (finalPage < 1 || finalPage > maxPage) {
        return;
    }
    show(finalPage);
}

// 设置左下角字
function setPageFront(startPage, endPage, sumPage){
    $('#startPage').text(startPage);
    $('#endPage').text(endPage);
    $('#sumPage').text(sumPage);
}

// 设置选项框
function setCheckAll(e){
    var pd = $(e).prop('checked');
    $(".checkBox").each(function () {
        $(this).prop('checked', pd);
    });
}



